package com.aimosheng.main.activity;

import android.app.Dialog;
import android.view.View;
import android.view.ViewGroup;

import com.aimosheng.beauty.interfaces.IBeautyViewHolder;
import com.aimosheng.beauty.utils.MhDataManager;
import com.aimosheng.beauty.utils.SimpleDataManager;
import com.aimosheng.beauty.views.BeautyViewHolder;
import com.aimosheng.beauty.views.SimpleBeautyViewHolder;
import com.aimosheng.common.CommonAppConfig;
import com.aimosheng.common.activity.AbsActivity;
import com.aimosheng.common.utils.DialogUitl;
import com.aimosheng.common.utils.WordUtil;
import com.aimosheng.im.utils.ChatLiveImUtil;
import com.aimosheng.main.R;
import com.aimosheng.one.views.ChatLivePushTxViewHolder;

/**
 * 美颜预设
 */
public class BeautyPreviewActivity extends AbsActivity implements View.OnClickListener {

    private ViewGroup mRoot;
    private View mWrap;
    protected ChatLivePushTxViewHolder mPushViewHolder;
    private IBeautyViewHolder mLiveBeautyViewHolder;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_beauty_preview;
    }

    @Override
    protected void main() {
        mRoot = findViewById(R.id.root);
        mPushViewHolder = new ChatLivePushTxViewHolder(mContext, (ViewGroup) findViewById(R.id.container));
        mPushViewHolder.setBig(true);
        mPushViewHolder.addToParent();
        mPushViewHolder.subscribeActivityLifeCycle();
        mPushViewHolder.startCameraPreview();
        mWrap = findViewById(R.id.wrap);
        findViewById(R.id.btn_beauty).setOnClickListener(this);
        findViewById(R.id.btn_save).setOnClickListener(this);
        ChatLiveImUtil.getInstance().setNoAccept(true);
    }


    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.btn_beauty) {
            beauty();
        } else if (i == R.id.btn_save) {
            new DialogUitl.Builder(mContext)
                    .setContent(WordUtil.getString(R.string.save_beauty_config))
                    .setConfrimString(WordUtil.getString(R.string.save))
                    .setCancelable(true)
                    .setClickCallback(new DialogUitl.SimpleCallback() {
                        @Override
                        public void onConfirmClick(Dialog dialog, String content) {
                            save();
                        }
                    })
                    .build()
                    .show();

        }
    }

    /**
     * 保存美颜数值
     */
    private void save() {
        if (CommonAppConfig.getInstance().isMhBeautyEnable()) {
            MhDataManager.getInstance().saveBeautyValue();
        } else {
            SimpleDataManager.getInstance().saveBeautyValue();
        }
    }


    /**
     * 设置美颜
     */
    public void beauty() {
        if (mLiveBeautyViewHolder == null) {
            if (CommonAppConfig.getInstance().isMhBeautyEnable()) {
                mLiveBeautyViewHolder = new BeautyViewHolder(mContext, mRoot);
            } else {
                mLiveBeautyViewHolder = new SimpleBeautyViewHolder(mContext, mRoot);
            }
            mLiveBeautyViewHolder.setVisibleListener(new IBeautyViewHolder.VisibleListener() {
                @Override
                public void onVisibleChanged(boolean visible) {
                    if (mWrap != null) {
                        if (visible) {
                            if (mWrap.getVisibility() == View.VISIBLE) {
                                mWrap.setVisibility(View.INVISIBLE);
                            }
                        } else {
                            if (mWrap.getVisibility() != View.VISIBLE) {
                                mWrap.setVisibility(View.VISIBLE);
                            }
                        }
                    }
                }
            });
        }
        mLiveBeautyViewHolder.show();
    }

    @Override
    public void onBackPressed() {
        if (mLiveBeautyViewHolder != null && mLiveBeautyViewHolder.isShowed()) {
            mLiveBeautyViewHolder.hide();
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onDestroy() {
        ChatLiveImUtil.getInstance().setNoAccept(false);
        if (mPushViewHolder != null) {
            mPushViewHolder.release();
        }
        mLiveBeautyViewHolder = null;
        super.onDestroy();
    }


}
